
#########################################################################################################
# 
# Top-Up Purchases and the Peak Price Effect
# ------------------------------------------
# This script explores the behavior of investors making additional purchases (top-ups) of stocks.
# If investors believe that stocks that have declined since their peak price will eventually recover, 
# they may be more inclined to top-up those stocks, especially when the losses since the peak are significant.

# Figure 5 presents binned scatterplots illustrating the relationships between losses since purchase, 
# losses since peak, and the likelihood of investors topping up their current positions with new purchases of the same stock.

# Regression Analysis:
# --------------------
# - **Table A40:** Presents regression estimates for the likelihood of topping up a stock as a function of the return since purchase.
# - **Table A41:** Presents regression estimates for the likelihood of topping up a stock as a function of the return since peak price.

# Output Files:
# -------------
# **Figure 5: Stock Top-Ups, Returns Since Purchase and Returns Since Peak**
# - `figures/TOPUP_basic_DE_purch_MAXC_peak_5update_loginsample.pdf`
# - `figures/TOPUP_DE_since_MAXC_peak_5update_loginsample.pdf`

# **Table A40: Stock Top-Up Behavior When Stocks Are in Loss Since Purchase (OLS and Fixed Effects Estimates)**
# - `tables/clean_TOPUP_returns_since_purchase_MAXC_peak_5update_loginsample.tex`

# **Table A41: Top-Up Behavior When Stocks Are in Loss Since Past Peak Price (OLS and Fixed Effects Estimates)**
# - `tables/clean_TOPUP_returns_since_peak_MAXC_peak_5update_loginsample.tex`
#
#########################################################################################################

{
  data_for_peaks[buy==1 & sell==0 & net.posit >=posit_yest,.(net.posit, posit_yest, port_date, last_td, narrative)]
  
  data_for_peaks[is.na(flag_no_peak),percentiles50:=xtile(return.since.pur,n=50)]
  data_for_peaks[is.na(flag_no_peak),percentiles_mean50:=mean(return.since.pur100), by=.(percentiles50)]
  
  
  data_for_peaks[is.na(flag_no_peak),percentiles_point50:=xtile(return.since.point,n=50)]
  data_for_peaks[is.na(flag_no_peak),percentiles_mean_point50:=mean(return.since.point100), by=.(percentiles_point50)]
  

  # Figure 5: Stock Top-Ups, Returns Since Purchase and Return Since Peak
  # Panel A: Stock Disposition Effect Since Purchase for Top-Ups

  variable= list("percentiles_mean50")
  
  data.plot3 <- 
    data_for_peaks [is.na(flag_no_peak) ] %>%
    group_by( .dots=variable) %>%
    dplyr::summarise(avg_PctPasses = ci.binom(buy)[1], 
                     lci_PctPasses = ci.binom(buy)[2], 
                     uci_PctPasses = ci.binom(buy)[3]) 
  data.plot3<- as.data.table(data.plot3)

  xlab_use="Return Since Purchase (%)"
  
  ggplot(data.plot3[percentiles_mean50> -50 & percentiles_mean50< 0][avg_PctPasses!=0]
         , aes_string(x =  
                        "percentiles_mean50",
                      y = "avg_PctPasses",
                      ymin = "lci_PctPasses", ymax = "uci_PctPasses")) + 
    geom_point(colour = "black") + 
    geom_linerange(colour = "black") +
    theme_bw() + theme_classic() +
    xlab(xlab_use) +
    ylab("Probability of Top-Up Stock") +
    theme(text = element_text(, 
                              size=19, family="serif"   ),
          legend.position = "bottom",
          legend.title.align = .5 ,
          legend.text=element_text(size=19),
          panel.border = element_blank()  ,
          panel.grid = element_blank() ,
          panel.grid.minor = element_blank()  
    ) +  scale_x_continuous(breaks=seq(-100,100,10) , limits=c(-50, 0)) #+

  setwd(wd_figures)
  
  name_plot= paste0("TOPUP_basic_DE_purch_", plot_name, "_", use_days ,"sample.pdf")
  ggsave(name_plot, width = 10, height = 5)
  

  # Figure 5: Stock Top-Ups, Returns Since Purchase and Return Since Peak
  # Panel B: Stock Disposition Effect Since Peak for Top-Ups


  variable="percentiles_mean_point50"
  
  data.plot <- 
    data_for_peaks[is.na(flag_no_peak)] %>%
    group_by( .dots=variable) %>%
    dplyr::summarise(avg_PctPasses = ci.binom(buy)[1], 
                     lci_PctPasses = ci.binom(buy)[2], 
                     uci_PctPasses = ci.binom(buy)[3]) 
  
  data.plot<- as.data.table(data.plot)
  

  xlab_use="Return Since Peak (%)"
  plot_saved <-  ggplot(data.plot[percentiles_mean_point50> -50 & percentiles_mean_point50< 50][avg_PctPasses!=0]
                        , aes_string(x =  
                                       "percentiles_mean_point50",
                                     y = "avg_PctPasses",
                                     ymin = "lci_PctPasses", ymax = "uci_PctPasses")) + 
    geom_point(colour = "black") + 
    geom_linerange(colour = "black") +
    theme_bw() + theme_classic() +
    xlab(xlab_use) +
    ylab("Probability of Top-Up Stock") +
    theme(text = element_text(, 
                              size=19, family="serif"   ),
          legend.position = "bottom",
          legend.title.align = .5 ,
          legend.text=element_text(size=19),
          panel.border = element_blank()  ,
          panel.grid = element_blank() ,
          panel.grid.minor = element_blank()  
    ) +  scale_x_continuous(breaks=seq(-100,100,10) , limits=c(-50, 0)) #+

  setwd(wd_figures)
  plot_saved 
  
  name_plot= paste0("TOPUP_DE_since_", plot_name, "_", use_days ,"sample.pdf")
  ggsave(name_plot, width = 10, height = 5)
  
  
# Regressions
  
  data_for_peaks[, female:=ifelse(gender=="F", 1, 0)]
  
  data_for_peaks[,port_value10000:=portfolio_value_all_stocks/10000]
  extreme.pv=data.table(sum_up(data_for_peaks[,.(port_value10000)], d=T))
  data_for_peaks[, outlier.pv:=0]
  
  data_for_peaks[port_value10000< extreme.pv$p1 | port_value10000> extreme.pv$p99, outlier.pv:=1]
  data_for_peaks[,.N, by=.(outlier.pv)]
  data_for_peaks[, distance_pur100:=distance_pur/100]
  data_for_peaks[, distance_peak100:=distance_peak/100]
  data_for_peaks[, age10:=age/10]
  data_for_peaks[, Count_sedols10:=Count_sedols/10]
  
  
  
  
  library(lfe)
  library(stargazer)
  
  summary(m1a<- felm(buy~  return.since.pur_neg100   | 0 | 0 | anon + port_date, 
                     data_for_peaks[gain.since.pur==0]))

  summary(m1b<- felm(buy~ return.since.pur_neg100  +   distance_pur100 +  port_value10000 +  Count_sedols10  +  account_tenure_years_to2016 +  female +  age10               | 0 | 0 | anon + port_date, data_for_peaks[gain.since.pur==0 & outlier.pv==0]))

  summary(m1c<- felm(buy~return.since.pur_neg100 +   distance_pur100 + port_value10000 +  Count_sedols10             | anon + Code_used_DS | 0 | anon + port_date, data_for_peaks[gain.since.pur==0 & outlier.pv==0]))
  
  
  summary(m2a<- felm(buy~ return.since.point_neg100    | 0 | 0 | anon + port_date, 
                     data_for_peaks[gain.since.point==0]))
  
  summary(m2b<- felm(buy~ return.since.point_neg100  +   distance_peak100 +  port_value10000 +  Count_sedols10  +  account_tenure_years_to2016 +  female +  age10               | 0 | 0 | anon + port_date, data_for_peaks[gain.since.point==0 & outlier.pv==0]))
    
  summary(m2c<- felm(buy~ return.since.point_neg100  +   distance_peak100 +  port_value10000 +  Count_sedols10                | anon  + Code_used_DS | 0 | anon + port_date, data_for_peaks[gain.since.point==0 & outlier.pv==0]))
    
  


  names_of_IV<-  unique(names(unlist(lapply(list(m1a, m1b, m1c), coef))))[-1]
  
  
  important_labels<- c(
    "Return Since Purchase $<0$ $(\\%)$",
    "Days Since Purchase (100 days)",
    "Portfolio Value (\\pounds10000)",
    "Number of Stocks (10 stocks)",
    "Account Tenure (years)",
    "Female=1",
    "Age (10 years)")
  
  # Table A40: Stock Top-Up Behavior When Stocks Are in Loss Since Purchase (OLS and Fixed Effects Estimates)
  
  order_IV_omited<- c()
  table<-stargazer(m1a, m1b, m1c, 
                   title="", align=TRUE,type="latex",  #digits = 5,
                   dep.var.labels=c(""),
                   covariate.labels=important_labels,
                   omit        = NULL  ,         
                   omit.stat=c("LL","ser","f", "adj.rsq") , 
                   omit.table.layout = "n",
                   float=F, 
                   table.layout ="-dc#-tas-",
                   style = "aer",
                   column.sep.width = "-1pt",
                   add.lines=list(c("Account FE", "NO", "NO",  "YES"),
                                  c("Stock FE",   "NO", "NO", "YES")), 
                   digits=4
  ) 
  
  
  
  library(stringr)
  table <- str_replace_all(table, "\\^", "")
  table <- str_replace_all(table, "R\\$\\{2\\}\\$", "R$^{2}$")
  setwd(wd_tables)
  write.table(table[10:(length(table)-2)], col.names = F, 
              row.names = F, quote = FALSE, paste0("clean_TOPUP_returns_since_purchase_", plot_name, "_", use_days ,"sample.tex"))
  
  
 # Table A41: Top-Up Behavior When Stocks Are in Loss Since Past Peak Price (OLS and Fixed Effects Estimates)
  
  names_of_IV<-  unique(names(unlist(lapply(list(m2a, m2b, m2c), coef))))[-1]
  
  

  important_labels<- c(
    "Return Since Peak $<0$ $(\\%)$",
    "Days Since Peak (100 days)",
    "Portfolio Value (\\pounds10000)",
    "Number of Stocks (10 stocks)",
    "Account Tenure (years)",
    "Female=1",
    "Age (10 years)")
  

  table<-stargazer( m2a, m2b, m2c,
                    title="", align=TRUE,type="latex",
                    dep.var.labels=c(""),
                    covariate.labels=important_labels,
                    omit        = NULL ,  
                    omit.stat=c("LL","ser","f", "adj.rsq") , 
                    omit.table.layout = "n",
                    float=F, 
                    table.layout ="-dc#-tas-",
                    style = "aer",
                    column.sep.width = "10pt",
                    add.lines=list(c("Account FE", "NO", "NO",  "YES"),
                                   c("Stock FE",   "NO", "NO", "YES")), #
                    digits=4 
  )
  
  
  library(stringr)
  table <- str_replace_all(table, "\\^", "")
  table <- str_replace_all(table, "R\\$\\{2\\}\\$", "R$^{2}$")
  
  
  setwd(wd_tables)
  write.table(table[10:(length(table)-2)], col.names = F, 
              row.names = F, quote = FALSE, paste0("clean_TOPUP_returns_since_peak_", plot_name, "_", use_days ,"sample.tex"))
  
}
